package com.bobohost.crmrbac.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.ui.Model;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bobohost.crmrbac.pojo.po.Department;
import com.bobohost.crmrbac.pojo.po.QueryObject;
import com.bobohost.crmrbac.service.DepartmentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping ("/department")
@RequiredArgsConstructor
@Slf4j


public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;
//
//    @GetMapping("/department")
//    public Department  getDepartmentById(int id) {
//        return departmentService.getDepartmentById(id);
//    }

    //获取department数据
    @GetMapping("/listall")
    @ResponseBody
    public List listAll(){
        List<Department> departmentList = departmentService.findDepartmentList();
        return departmentList;

    }
//    //根据id查询部门
//    @GetMapping("/findListById")
//    @ResponseBody
//    public Department findListById(int id){
//        return departmentService.findDepartmentById(id);
//    }
    @RequestMapping("/list")
    public String list( Model model,@ModelAttribute("qo")QueryObject queryObject){
        IPage page = Page.of(queryObject.getCurrentPage(), queryObject.getPageSize());
        // 调用Department服务的分页查询方法，获取Department列表
        IPage<Department> pageResult= departmentService.findDepartmentListPage(page);

        // 向模型中添加属性名为 "pageInfo" 的对象，以便在视图中使用

        Map<String,Object> pageInfo=new HashMap<>();
        pageInfo.put("pages",pageResult.getPages());
        pageInfo.put("pageNum",pageResult.getCurrent());
        pageInfo.put("list",pageResult.getRecords());


        model.addAttribute("pageInfo",pageInfo);
        //
        return "department/list";
    }

    //添加保存一个部门
    @PostMapping ("/saveOrUpdate")
    @RequiresRoles("Admin_MGR")
    public String saveOrUpdate(Department department){
        departmentService.saveOrUpdateDepartment(department);
        log.debug("保存部门{}",department);
        return "redirect:/department/list";
    }

     @RequestMapping("/delete")
    public String delete( Integer id){
        departmentService.removeById(id);
        return "redirect:/department/list";
    }

//
//    //更新一个部门
//    @GetMapping("/update")
//    public String update(Department department){
//        departmentService.updateDepartment(department);
//        return "redirect:/department/list";
//    }
//    //删除一个部门
//    @GetMapping("/delete")
//    public String delete(int id){
//        departmentService.removeById(id);
//        return "redirect:/department/list";
//    }
//




}
